package prj.common.web.component;

import java.util.ArrayList;
import java.util.List;

import javax.el.ValueExpression;
import javax.faces.component.html.HtmlGraphicImage;

public class CaptchaComponent extends HtmlGraphicImage {
    public static final String COMPONENT_TYPE = "prj.common.Captcha";

    public CaptchaComponent() {
        super();
        setRendererType("prj.common.CaptchaRenderer");
    }
    
    protected enum PropertyKeys {
    	captchaField;
    	
        String toString;
        PropertyKeys(String toString) { this.toString = toString; }
        PropertyKeys() { }
        public String toString() {
            return ((toString != null) ? toString : super.toString());
        }
    }

    public String getCaptchaField() {
        return (String) getStateHelper().eval(PropertyKeys.captchaField);

    }

    /**
     * <p>Set the value of the <code>alt</code> property.</p>
     */
    public void setCaptchaField(String captchaField) {
        getStateHelper().put(PropertyKeys.captchaField, captchaField);
        handleAttribute("captchaField", captchaField);
    }


    private void handleAttribute(String name, Object value) {
        List<String> setAttributes = (List<String>) this.getAttributes().get("javax.faces.component.UIComponentBase.attributesThatAreSet");
        if (setAttributes == null) {
            setAttributes = new ArrayList<String>(6);
            this.getAttributes().put("javax.faces.component.UIComponentBase.attributesThatAreSet", setAttributes);
        }
        if (setAttributes != null) {
            if (value == null) {
                ValueExpression ve = getValueExpression(name);
                if (ve == null) {
                    setAttributes.remove(name);
                }
            } else if (!setAttributes.contains(name)) {
                setAttributes.add(name);
            }
        }
    }
}
